<?php // 4.0
class customers_new {
  function loaddata() {
    global $masterclass;

    $masterclass->loadmodule('customers');
    $masterclass->loadmodule('personnel');
    $masterclass->loadmodule('invoices');

    $masterclass->websiteconfig['title'].=' - '.$masterclass->allclasses['personnel']->fetch_personnelname($masterclass->site['personnelid']);
  return;
  }
  
  function savedata() {
    global $masterclass;
    
    foreach ($masterclass->sitevars['post_action'] as $postaction) {
      switch ($postaction) {
       case '':
       
      break;
      }
    }
     
  return;
  }
  function invoices_removeinvoice() {
    global $masterclass;

    if (isset($_POST['rsargs'])) {
      $masterclass->sitevars['invoicesid']=$_POST['rsargs'][1];
    }
    $dbinfo = array(
      'table'=>'invoices_documents',
      'trackingid'=>'invoicesid',
      'static'=>array('active'=>'0')
    );
    $masterclass->uploaddata($dbinfo);
  return;
  }
  function invoices_savepayment() {
    global $masterclass;

    $invoiceid=$_POST['rsargs'][1];
    $paymentdate=$_POST['rsargs'][2];

    $masterclass->sitevars['invoicesid']=$invoiceid;

    $dbinfo = array(
      'table'=>'invoices_documents',
      'trackingid'=>'invoicesid',
      'static'=>array($paymentdate=>'paid')
    );
    $masterclass->uploaddata($dbinfo);

  return;
  }
  function display_customerlist($containername='',$listview='',$pg='') {
    global $masterclass;

    if ($pg<0) { $pg=1; }

    switch ($listview) {
     default:
     case 'all':
      $strwhere=" (`active`='1') ";
      $headline='All Customers';
    break;
    }

    $listcnt=$masterclass->fetch_recordcount('customers',$strwhere);
    $listpgcount=ceil($listcnt/$masterclass->websiteconfig['entriesperpage']);
    $listarray=$masterclass->fetch_records('customers',$pg,$strwhere,' `updated` DESC, `first`,`last` ');

    $ret='';
    $ret.='<div class="sitebox_navigation" style="text-align:center;font-weight:800;border-bottom:1px solid;"><b>'.$headline.'</b></div>';
    $ret.='<table width="100%" align="center" cellpadding="0" cellspacing="0" class="sitebox_content">';
    if (empty($listarray)) {
      $ret.='<tr valign="top">';
      $ret.='<td width="100%" valign="middle" style="padding-left:2px;text-align:center;">No Customers</td>';
      $ret.='</tr>';
    } else {
      foreach ($listarray as $cl) {

        $openclick=$masterclass->mainconfig['dir'].$masterclass->mainconfig['htmldirectory'].'invoices/quote.php?customerid='.$cl['id'];

        $ret.='<tr valign="top" '.$trclass.' '.$onmouseover.' style="cursor:pointer;">';
        $ret.='<td valign="middle" style="padding-left:2px;text-align:left;border-bottom:1px solid;"><nobr><a href="'.$openclick.'">'.$cl['first'].' '.$cl['last'].'</a></nobr>&nbsp;</td>';
        $ret.='</tr>';
      }
    }
    $ret.='</table>';

    $arrret=array(
    'load_list'=>$ret,
    'load_listpage'=>$pg,
    'load_listpagecnt'=>$listpgcount,
    'load_listcnt'=>$listcnt,
    'load_containername'=>$containername
    );

    $masterclass->site['grabdata_vars']['load_list']=$ret;
    $masterclass->site['grabdata_vars']['load_listpage']=$pg;
    $masterclass->site['grabdata_vars']['load_listpagecnt']=$listpgcount;
    $masterclass->site['grabdata_vars']['load_listcnt']=$listcnt;
    $masterclass->site['grabdata_vars']['load_containername']=$containername;

  return $arrret;
  }
  function display_invoices($containername='',$listview='',$pg='') {
    global $masterclass;

    if (isset($_POST['rsargs']) && !empty($_POST['rsargs'])) {
      $containername=$_POST['rsargs'][1];
      $listview=$_POST['rsargs'][2];
      $pg=$_POST['rsargs'][3];
    }

    if ($pg<0) { $pg=1; }

    switch ($listview) {
     default:
      $strwhere=" (`active`='1') ";
      $strorderby=' ABS(`documentid`) DESC, `documentdate` DESC, `updated` DESC ';
     break;
    }
    $listcnt=$masterclass->fetch_recordcount('invoices_documents',$strwhere);
    $listpgcount=ceil($listcnt/$masterclass->websiteconfig['entriesperpage']);
    $listarray=$masterclass->fetch_records('invoices_documents',$pg,$strwhere,$strorderby);

    $ret='';

    $ret.='<table width="100%" align="center" cellpadding="0" cellspacing="0" class="sitebox_content">';
    if (empty($listarray)) {
      $ret.='<tr valign="top">';
      $ret.='<td width="100%" valign="middle" style="padding-left:2px;text-align:center;">No Invoices</td>';
      $ret.='</tr>';
    } else {
      foreach ($listarray as $l) {
        $csstype=$l['documenttype'];
        if ($l['paid']=='0000-00-00' || $l['paid']=='') { $csstype='unpaid'; }
        $invoicetotals=$masterclass->allclasses['invoices']->calc_invoicetotal($masterclass->allclasses['invoices']->fetch_invoices_items($l['id']));
  
        $onmouseover=' OnMouseOver="this.className=\'sitebox_content_mouseover\';" OnMouseOut="this.className=\'sitebox_invoiceslist_'.$csstype.'\';" ';

        $trclass=' class="sitebox_invoiceslist_'.$csstype.'" ';
        $tdclass=' class="sitebox_invoiceslist_'.$csstype.'" ';
        
        $removeitemclick='OnClick="if (confirm(\'Remove Invoice\\nInvoice #'.$l['documentid'].'?\')) { invoices_remove_invoice(\'invoices_removeinvoice\',\''.$l['id'].'\'); }" ';

        $ret.='<tr valign="top" '.$trclass.' '.$onmouseover.' style="cursor:pointer;">';

        if (strtolower($l['documenttype'])=='invoice') {
          $openclick=' OnClick="openwindow(\''.$masterclass->mainconfig['dir'].$masterclass->mainconfig['htmldirectory'].'invoices/showinvoice.php?invoicesid='.$l['id'].'\',\'invoices_show'.$l['id'].'\',\'760\',\'600\');" ';
          $emailinvoiceclick=' OnClick="openwindow(\''.$masterclass->mainconfig['dir'].$masterclass->mainconfig['htmldirectory'].'invoices/showinvoice_emailpdf.php?invoicesid='.$l['id'].'\',\'invoices_emailpdf'.$l['id'].'\',\'540\',\'360\');" ';
          $pdfclick=' OnClick="openwindow(\''.$masterclass->mainconfig['dir'].$masterclass->mainconfig['htmldirectory'].'invoices/showinvoice_pdf.php?nohtml=1&invoicesid='.$l['id'].'\',\'invoices_pdf'.$l['id'].'\',\'540\',\'360\');" ';

          $ret.='<td '.$openclick.' width="20" valign="middle" style="padding-left:3px;text-align:left;border-bottom:1px solid;border-right:1px solid;cursor:pointer;"><img src="'.$masterclass->mainconfig['dir'].'art/book_go.png" border="0" alt="Open" title=" Open "></td>';
          $ret.='<td '.$pdfclick.' width="20" valign="middle" style="padding-left:3px;text-align:left;border-bottom:1px solid;border-right:1px solid;cursor:pointer;"><img src="'.$masterclass->mainconfig['dir'].'art/page_white_acrobat.png" border="0"  alt="Open PDF" title=" Open PDF "></td>';
          $ret.='<td '.$emailinvoiceclick.' width="20" valign="middle" style="padding-left:3px;text-align:left;border-bottom:1px solid;border-right:1px solid;cursor:pointer;"><img src="'.$masterclass->mainconfig['dir'].'art/email_go.png" border="0" alt="E-Mail" title=" E-Mail "></td>';
          //$ret.='<td '.$paymentinvoiceclick.' width="20" valign="middle" style="padding-left:3px;text-align:left;border-bottom:1px solid;border-right:1px solid;cursor:pointer;"><img src="'.$masterclass->mainconfig['dir'].'art/money_dollar.png" border="0" alt="'.$paymenttitle.'" title=" '.$paymenttitle.' "></td>';
          $ret.='<td '.$removeitemclick.' width="20" valign="middle" style="padding-left:3px;text-align:left;border-bottom:1px solid;border-right:1px solid;cursor:pointer;"><img src="'.$masterclass->mainconfig['dir'].'art/cross.png" border="0" alt="Delete" title=" Delete "></td>';

        } else {
          $openclick=' OnClick="openwindow(\''.$masterclass->mainconfig['dir'].$masterclass->mainconfig['htmldirectory'].'invoices/quote.php?invoicesid='.$l['id'].'\',\'invoices_show'.$l['id'].'\',\'760\',\'600\');" ';

          $ret.='<td '.$openclick.' width="20" valign="middle" style="padding-left:3px;text-align:left;border-bottom:1px solid;border-right:1px solid;cursor:pointer;"><img src="'.$masterclass->mainconfig['dir'].'art/book_go.png" border="0" alt="Open" title=" Open "></td>';
          $ret.='<td '.$openclick.' colspan="2" style="padding-left:3px;text-align:left;border-bottom:1px solid;border-right:1px solid;">'.substr(strtoupper($l['documenttype']),0,4).'&nbsp;</td>';
          $ret.='<td '.$removeitemclick.' width="20" valign="middle" style="padding-left:3px;text-align:left;border-bottom:1px solid;border-right:1px solid;cursor:pointer;"><img src="'.$masterclass->mainconfig['dir'].'art/cross.png" border="0" alt="Delete" title=" Delete "></td>';
        }
            

        $ret.='<td '.$openclick.' '.$tdclass.' width="15" valign="middle" style="padding-left:5px;text-align:left;border-bottom:1px solid;"><nobr>#'.$l['documentid'].'</nobr>&nbsp;&nbsp;</td>';
        $ret.='<td '.$openclick.' '.$tdclass.' valign="middle" style="padding-left:5px;text-align:left;border-bottom:1px solid;"><nobr>'.$l['recipientlineone'].'</nobr>&nbsp;&nbsp;</td>';
        if ($l['customer']!='' && $l['customer']!=0) {
          $opencustomerclick=' OnClick="openwindow(\''.$masterclass->mainconfig['dir'].$masterclass->mainconfig['htmldirectory'].'invoices/quote.php?customerid='.$l['id'].'\',\'_blank\',\'760\',\'600\');"  ';
          $customername=$masterclass->allclasses['customers']->fetch_customername($l['customer']);
        } else {
          $opencustomerclick='';
          $customername='&nbsp;';
        }
        //$ret.='<td '.$opencustomerclick.' '.$tdclass.' width="25" valign="middle" style="padding-left:2px;text-align:left;border-bottom:1px solid;"><nobr>'.$customername.'</nobr>&nbsp;&nbsp;</td>';
        
        $ret.='<td '.$openclick.' '.$tdclass.' width="50" valign="middle" style="padding-left:5px;padding-right:2px;text-align:right;border-bottom:1px solid;"><nobr>'.date("m-d-Y",strtotime($l['documentdate'])).'</nobr>&nbsp;</td>';
        $ret.='<td '.$openclick.' '.$tdclass.' width="50" valign="middle" style="padding-left:5px;padding-right:2px;text-align:right;border-bottom:1px solid;"><nobr>'.date("m-d-Y",strtotime($l['duedate'])).'</nobr>&nbsp;</td>';
        $ret.='<td '.$openclick.' '.$tdclass.' width="75" valign="middle" style="padding-left:5px;padding-right:2px;text-align:right;border-bottom:1px solid;"><nobr>$'.number_format($invoicetotals['total'],2).'</nobr>&nbsp;&nbsp;</td>';
        $ret.='</tr>';
      }
    }
    $ret.='</table>';
    
    $arrret=array(
    'load_list'=>$ret,
    'load_listpage'=>$pg,
    'load_listpagecnt'=>$listpgcount,
    'load_listcnt'=>$listcnt,
    'load_containername'=>$containername
    );

    $masterclass->site['grabdata_vars']['load_list']=$ret;
    $masterclass->site['grabdata_vars']['load_listpage']=$pg;
    $masterclass->site['grabdata_vars']['load_listpagecnt']=$listpgcount;
    $masterclass->site['grabdata_vars']['load_listcnt']=$listcnt;
    $masterclass->site['grabdata_vars']['load_containername']=$containername;

  return $arrret;
  }
  function display_invoicesdue($containername='',$listview='',$pg='') {
    global $masterclass;

    if (isset($_POST['rsargs']) && !empty($_POST['rsargs'])) {
      $containername=$_POST['rsargs'][1];
      $listview=$_POST['rsargs'][2];
      $pg=$_POST['rsargs'][3];
    }

    if ($pg<0) { $pg=1; }

    switch ($listview) {
     default:
      if ($strwhere!='') { $strwhere.=' AND '; }
      $strwhere.=" (`active`='1') AND (`duedate`='".date("Y-m-d")."'  AND `paid`='0000-00-00') AND (`documenttype`='invoice') ";
      $strorderby=' ABS(`documentid`) DESC, `documentdate` DESC, `updated` DESC ';
     break;
     case 'overdue':
      if ($strwhere!='') { $strwhere.=' AND '; }
      $strwhere.=" (`active`='1') AND (`duedate`<'".date("Y-m-d")."' AND `paid`='0000-00-00') AND (`documenttype`='invoice') ";
      $strorderby=' `duedate` DESC ';
     break;
    }
    $listcnt=$masterclass->fetch_recordcount('invoices_documents',$strwhere);
    $listpgcount=ceil($listcnt/$masterclass->websiteconfig['entriesperpage']);
    $listarray=$masterclass->fetch_records('invoices_documents',$pg,$strwhere,$strorderby);

    $ret='';

    $ret.='<table width="100%" align="center" cellpadding="0" cellspacing="0" class="sitebox_content">';
    if (empty($listarray)) {
      $ret.='<tr valign="top">';
      $ret.='<td width="100%" valign="middle" style="padding-left:2px;text-align:center;">No Invoices</td>';
      $ret.='</tr>';
    } else {
      foreach ($listarray as $l) {
        $invoicetotals=$masterclass->allclasses['invoices']->calc_invoicetotal($masterclass->allclasses['invoices']->fetch_invoices_items($l['id']));
        
        $trclass=' class="sitebox_invoiceslist_'.$csstype.'" ';
        $tdclass=' class="sitebox_invoiceslist_'.$csstype.'" ';
        
        $onmouseover=' OnMouseOver="this.className=\'sitebox_content_mouseover\';" OnMouseOut="this.className=\'sitebox_content\';" ';
        
        $ret.='<tr valign="top" '.$trclass.' '.$onmouseover.' style="cursor:pointer;">';
        
        if (strtolower($l['documenttype'])=='invoice') {
          $openclick=' OnClick="openwindow(\''.$masterclass->mainconfig['dir'].$masterclass->mainconfig['htmldirectory'].'invoices/showinvoice.php?invoicesid='.$l['id'].'\',\'invoices_show'.$l['id'].'\',\'760\',\'600\');" ';
          $emailinvoiceclick=' OnClick="openwindow(\''.$masterclass->mainconfig['dir'].$masterclass->mainconfig['htmldirectory'].'invoices/showinvoice_emailpdf.php?invoicesid='.$l['id'].'\',\'invoices_emailpdf'.$l['id'].'\',\'540\',\'360\');" ';
          $pdfclick=' OnClick="openwindow(\''.$masterclass->mainconfig['dir'].$masterclass->mainconfig['htmldirectory'].'invoices/showinvoice_pdf.php?nohtml=1&invoicesid='.$l['id'].'\',\'invoices_pdf'.$l['id'].'\',\'540\',\'360\');" ';

          $ret.='<td '.$openclick.' width="20" valign="middle" style="padding-left:3px;text-align:left;border-bottom:1px solid;border-right:1px solid;cursor:pointer;"><img src="'.$masterclass->mainconfig['dir'].'art/book_go.png" border="0" alt="Open" title=" Open "></td>';
          $ret.='<td '.$pdfclick.' width="20" valign="middle" style="padding-left:3px;text-align:left;border-bottom:1px solid;border-right:1px solid;cursor:pointer;"><img src="'.$masterclass->mainconfig['dir'].'art/page_white_acrobat.png" border="0"  alt="Open PDF" title=" Open PDF "></td>';
          $ret.='<td '.$emailinvoiceclick.' width="20" valign="middle" style="padding-left:3px;text-align:left;border-bottom:1px solid;border-right:1px solid;cursor:pointer;"><img src="'.$masterclass->mainconfig['dir'].'art/email_go.png" border="0" alt="E-Mail" title=" E-Mail "></td>';

        } else {
          $openclick=' OnClick="openwindow(\''.$masterclass->mainconfig['dir'].$masterclass->mainconfig['htmldirectory'].'invoices/quote.php?invoicesid='.$l['id'].'\',\'invoices_show'.$l['id'].'\',\'760\',\'600\');" ';

          $ret.='<td '.$openclick.' width="20" valign="middle" style="padding-left:3px;text-align:left;border-bottom:1px solid;border-right:1px solid;cursor:pointer;"><img src="'.$masterclass->mainconfig['dir'].'art/book_go.png" border="0" alt="Open" title=" Open "></td>';
          $ret.='<td colspan="2" style="padding-left:3px;text-align:left;border-bottom:1px solid;border-right:1px solid;">&nbsp;</td>';
       }
        
        $ret.='<td '.$openclick.' '.$tdclass.' valign="middle" style="padding-left:5px;text-align:left;border-bottom:1px solid;"><nobr>#'.$l['documentid'].'&nbsp;'.$l['recipientlineone'].'</nobr>&nbsp;&nbsp;</td>';
        $ret.='<td '.$openclick.' '.$tdclass.' width="75" valign="middle" style="padding-left:5px;padding-right:2px;text-align:right;border-bottom:1px solid;"><nobr>$'.money_format("%i",$invoicetotals['total']).'</nobr>&nbsp;&nbsp;</td>';
        $ret.='</tr>';
      }
    }
    $ret.='</table>';
    
    $arrret=array(
    'load_list'=>$ret,
    'load_listpage'=>$pg,
    'load_listpagecnt'=>$listpgcount,
    'load_listcnt'=>$listcnt,
    'load_containername'=>$containername
    );

    $masterclass->site['grabdata_vars']['load_list']=$ret;
    $masterclass->site['grabdata_vars']['load_listpage']=$pg;
    $masterclass->site['grabdata_vars']['load_listpagecnt']=$listpgcount;
    $masterclass->site['grabdata_vars']['load_listcnt']=$listcnt;
    $masterclass->site['grabdata_vars']['load_containername']=$containername;

  return $arrret;
  }

  function display() {
  global $masterclass;

?>
<table width="100%" cellpadding="2" cellspacing="5" border="0">
<tr valign="top">
  <td width="100%">
<?php
/* Start Window */


  $customersdata=$this->display_customerlist($unquieid,'',1);
  echo '<div id="'.$unquieid.'">'.$customersdata['load_list'].'</div>';
/* End Window */
echo $masterclass->allclasses['websites']->sitebox_vspacer();
/* Start Window */


  $invoicelistdata=$this->display_invoices($unquieid,'',1);
  //echo '<div id="'.$unquieid.'">'.$invoicelistdata['load_list'].'</div>';

/* End Window */
?>
</td></tr></table>
<?php

  return;
  }
}
?>
